package com.dao.impl;

import java.util.List;

import com.dao.TreeDao;

public class TreeDaoImpl extends UniversalDaoImpl implements TreeDao {
	
	public List getListByType(int type, boolean enabled) {
		String sql;
		if (enabled)
			sql = "select code,parent_code,name,isLeaf,enabled from tree where type="
					+ type + " and enabled is " + enabled + " order by code";
		else
			sql = "select code,parent_code,name,isLeaf,enabled from tree where type="
					+ type + "  order by code";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	
	public List getListByParentCode(String code, int type, boolean enabled) {
		String sql;
		int len = code.length();
		if (enabled)
			sql = "select code id,parent_code,name text,isLeaf leaf,enabled from tree where type="
					+ type + " and left(code," + len + ")='" + code	+ "' and enabled is " + enabled + " order by code";
		else
			sql = "select code,parent_code,name,isLeaf,enabled from tree where type="
					+ type + " and left(code," + len + ")='" + code + "'  order by code";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	
	public List getListByParentCode(String code, int type, int level,
			boolean enabled) {
		String sql;
		int len = code.length();
		level = 3 * level;
		if (enabled)
			sql = "select code,parent_code,name,isLeaf,enabled from tree  where type="
					+ type + " and left(code," + len + ")='" + code + "' and length(code)<="
					+ level + "  and enabled is " + enabled + " order by code";
		else
			sql = "select code,parent_code,name,isLeaf,enabled from tree where type="
					+ type + " and left(code," + len + ")='" + code
					+ "' and length(code)<=" + level + "  order by code";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	
	public List getListTreeType() {
		String sql="select id,text,leaf from (select distinct (type) as id,typeName as  text,'true' leaf  from tree order by type) as t";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}

	
	public List getListTree(String code, int type) {
		String sql;
		if(code==null)
			sql = "select code id,parent_code,concat('[',code,'] ',name) text,isLeaf leaf,enabled from tree where type="
					+ type + " and parent_code is null and enabled is  true order by code";
		else
			sql = "select code id,parent_code,concat('[',code,'] ',name) text,isLeaf leaf,enabled from tree where type="
				+ type + " and parent_code='" + code+ "' and enabled is  true order by code";
		sql="select id,text,leaf from ("+sql+") as t";
		logger.info(sql);
		return getJdbcTemplate().queryForList(sql);
	}
	
}
